<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\MemberRecorder;
use app\models\PaylogRecorder;
use app\models\VipRecorder;

class MemberController extends BaseController{

    public function actionList(){
        $post = \Yii::$app->request->post('pagination');
        $page=intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $offset = $page*$pagesize;
        $list = MemberRecorder::find();
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['updatetime'] = date('Y-m-d H:i:s',$v['updatetime']);
            $list[$k]['user_name'] = empty($v['user_name'])?\Yii::$app->params['non_name']:$v['user_name'];
            $list[$k]['user_pic'] = empty($v['user_pic'])?\Yii::$app->params['base_url'].\Yii::$app->params['non_pic']:\Yii::$app->params['base_url'].$v['user_pic'];
            $vip = VipRecorder::find()->where('id='.$v['vip'])->one();
            $list[$k]['vip'] = empty($vip)?'无':$vip->vip_name;
        }
        $result = ['result'=>1,'data'=>$list,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = MemberRecorder::find()->select(['id','user_name','user_pic','user_tel'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model['user_pic'] = empty($model['user_pic'])?\Yii::$app->params['base_url'].\Yii::$app->params['non_pic']:\Yii::$app->params['base_url'].$model['user_pic'];
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSaveone(){
        $post = \Yii::$app->request->post('form');
        $model = MemberRecorder::find()->where('id=:id',[':id' => $post['id']])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->user_name = $post['user_name'];
        $model->user_tel = $post['user_tel'];
        $model->user_pic = $post['user_pic'];
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionChangestatus(){
        $id = \Yii::$app->request->post('id');
        $model = MemberRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->status=!$model->status;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionChangey(){
        $id = \Yii::$app->request->post('id');
        $model=MemberRecorder::find()->select(['id','user_name','pay_num'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result= ['result'=>0,'data'=>'不存在相关会员，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSavey(){
        $post = \Yii::$app->request->post('form');
        $id = $post['id'];
        $model = MemberRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>1,'data'=>'不存在相关会员，请重新操作'];
            return json_encode($result);
        }
        $y_num = doubleval($post['y_num']);
        if(empty($y_num)){
            $result = ['result'=>0,'data'=>'余额不能为空'];
            return json_encode($result);
        }
        $num = $model->pay_num+$y_num;
        $num = ($num<0)?0:$num;
        $model->pay_num=number_format($num,"2",".","");
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionChangejf(){
        $id = \Yii::$app->request->post('id');
        $model = MemberRecorder::find()->select(['id','user_name','jf_num'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSavejf(){
        $post = \Yii::$app->request->post('form');
        $id = intval($post['id']);
        $num = intval($post['num']);
        $model = MemberRecorder::find()->where('id=:id',[':id' => $id])->one();
        $num = $model->jf_num+$num;
        $num = ($num<0)?0:$num;
        $model->jf_num = $num;
        $model->save(false);
        $result= ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionViplist(){
        $list = VipRecorder::find()->select(['id','vip_name'])->asArray()->all();
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionChangevip(){
        $id = \Yii::$app->request->post('id');
        $model = MemberRecorder::find()->select(['id','user_name','vip'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSavevip(){
        $post = \Yii::$app->request->post('form');
        $id = $post['id'];
        $vip = intval($post['vip']);
        $model = MemberRecorder::find()->where('id=:id',[':id'=>$id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关会员，请重新操作'];
            return json_encode($result);
        }
        if($vip){


            //获取vip
            $vip_model = VipRecorder::find()->where("id=:id",[':id' => $vip])->one();
            if(!$vip_model){
                $result = ['result'=>0,'data'=>'不存在相关vip，可能已经删除，请重新操作'];
                return json_encode($result);
            }
            switch($vip_model->vip_time){
                case 0:
                    $time = strtotime("+ 100 years",time());
                    break;
                case 1:
                    $time = strtotime("+ 30 days",time());
                    break;
                case 2:
                    $time = strtotime("+ 90 days",time());
                    break;
                case 3:
                    $time = strtotime("+ 180 days",time());
                    break;
                case 4:
                    $time = strtotime("+ 1 years",time());
                    break;
            }
        }
        $model->vip_time = $time;
        $model->vip = $vip;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionXflist(){
        $post = \Yii::$app->request->post('page');
        $id = intval($post['xf_id']);
        $page = intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $offset = $page*$pagesize;
        $list = PaylogRecorder::find()->where('user_id=:id',[':id' => $id]);
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->orderBy('addtime DESC')->asArray()->all();
        $arr = [];
        foreach($list as $k=>$v){
            $arr[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $arr[$k]['id'] = $v['id'];
            $arr[$k]['user_id'] = $v['user_id'];
            $arr[$k]['content'] = $v['content'];
            $arr[$k]['order_id']  =$v['order_id'];
            switch($v['pay_type']){
                case 1:
                    $arr[$k]['type'] = "余额";
                    break;
                case 2:
                    $arr[$k]['type'] = "微信";
                    break;
                case 3:
                    $arr[$k]['type'] = "积分";
                    break;
                case 4:
                    $arr[$k]['type'] = "收银";
                    break;
                case 5:
                    $arr[$k]['type'] = "扫码";
                    break;
                case 6:
                    $arr[$k]['type'] = "充值";
                    break;
                case 7:
                    $arr[$k]['type'] = "平台";
                    break;
                default:
                    $arr[$k]['type'] = "其他";
                    break;
            }

        }
        $result = ['result'=>1,'data'=>$arr,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }



}